package com.itheima.service;

import com.itheima.exception.BusinessExecption;
import com.itheima.util.AgreementUtil;

import java.io.*;
import java.net.Socket;
import java.util.ResourceBundle;
import java.util.Scanner;

public class FileUoDownServiceImp implements FileUpDownService {
    public File current = new File("root");
    private ResourceBundle bundle;
    private String downloadPath;

    @Override
    public void start() {
        try {
            bundle = ResourceBundle.getBundle("yunpan");

            downloadPath = bundle.getString("DownloadPath");

            File downloadDir = new File(downloadPath);

            if (downloadDir.isFile()) {
                throw new BusinessExecption("文件不能当做下载目录，请更改下载路径配置！");
            } else if (!downloadDir.exists() && !downloadDir.mkdir()) {
                throw new BusinessExecption("下载目录初始化失败，请检查下载路径配置是否正确！");
            }

            Scanner sc = new Scanner(System.in);
            System.out.println("*******欢迎进入黑马网盘*******");

            while (true) {
                System.out.println("***************************************************************************");
                System.out.println("1)浏览当前目录 \t2)浏览子目录 \t3)返回上一级目录 \t4)下载文件 \t5)上传文件");
                System.out.println("***************************************************************************");
                String choice = sc.nextLine();
                switch (choice) {
                    case "1":
                        scanDirection(current);
                        break;
                    case "2":
                        System.out.println("请输入要浏览的子目录：");
                        String dir = sc.nextLine();
                        try {
                            scanDirection(new File(current, dir));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case "3":
                        if (current.getName().equals("root")) {
                            System.out.println("没有上一级了");
                        } else {
                            scanDirection(current.getParentFile());
                        }
                        break;
                    case "4":
                        System.out.println("请输入要下载的文件名换成呢个（含后缀）：");
                        String fileName = sc.nextLine();
                        downloadFile(new File(current, fileName));
                        break;
                    case "5":
                        while (true) {
                            System.out.println("请输入在计算机中要上传的文件路径");
                            String uploadFilePath = sc.nextLine();
                            File upFile = new File(uploadFilePath);
                            if (!upFile.exists()) {
                                System.out.println("文件不存在，请重新输入！");
                            } else if (upFile.isDirectory()) {
                                System.out.println("抱歉，不支持目录上传！");
                            } else if (upFile.isFile()) {
                                uploadFile(upFile);
                                break;
                            }
                        }
                        break;
                    default:
                        System.out.println("功能尚在开发中...");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void scanDirection(File path) {
        try (
                Socket socket = new Socket("127.0.0.1", 8888);
                InputStream netIn = socket.getInputStream();
                OutputStream netOut = socket.getOutputStream();
        ) {
            String scan = AgreementUtil.getAgreement("SCAN", path.getName(), null, null);
            AgreementUtil.sendAgreement(netOut, scan);

            System.out.println("====================等待服务器给回应消息===========================");

            BufferedReader br = new BufferedReader(new InputStreamReader(netIn));

            String content;

            String firstLine = br.readLine();
            String type = AgreementUtil.getStatus(firstLine);
            if (type.equals("OK")) {
                current = new File(AgreementUtil.getFileName(firstLine));
                System.out.println("当前目录：" + current);
                while ((content = br.readLine()) != null) {
                    System.out.println(content);
                }
            } else {
                System.out.println("浏览失败：" + AgreementUtil.getMessage(firstLine));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void downloadFile(File file) {

    }

    @Override
    public void uploadFile(File upfile) {

    }
}
